/* -*- c++ -*- */
/* 
 * Copyright 2013 <+YOU OR YOUR COMPANY+>.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifndef INCLUDED_BLOCKS_QAM_SNR_ESTIMATOR_IMPL_H
#define INCLUDED_BLOCKS_QAM_SNR_ESTIMATOR_IMPL_H

#include <blocks/qam_snr_estimator.h>
#include <digital_constellation.h>

namespace gr {
  namespace blocks {
    class protocol_phases {
    public:
    	enum phases {
            SKIP,
            KEY_FILL,
            KEY_SEARCH,
    		SNR_ESTIMATION,
    		FORWARDING
    	};
    };

    class qam_snr_estimator_impl : public qam_snr_estimator{
    private:
      protocol_phases::phases d_state;
	  unsigned int d_skip_counter;
	  unsigned int d_skip;
	  unsigned int d_counter;
	  unsigned int d_training_length;
      unsigned int d_key_length;
      unsigned int d_num_errs_allowed;
      unsigned int d_symbol_counter;
      int d_last_sym;
      int d_last_expec;
      int d_last;
      float d_sum_expected;
      float d_last_arg;
      float d_corr_sum;
      float d_corr_med;
      float d_corr;
      std::vector<int> d_key_seq;
      std::vector<int> d_key_search;
      std::vector<int> d_snr_seq;
	  const std::vector<gr_complex> d_constellation;
      bool d_is_differential;
      bool d_first;

      void log(gr_complex, gr_complex, int);
      int demap(gr_complex);
      float correlate_key();

    public:
      qam_snr_estimator_impl(
        unsigned int skip, 
	    const std::vector<int> &snr_seq,
	    const std::vector<gr_complex> &constellation,
	    const std::vector<int> &key_seq,
        int num_errs
      );
      ~qam_snr_estimator_impl();

      int work(int noutput_items,
	       gr_vector_const_void_star &input_items,
	       gr_vector_void_star &output_items);
    };

  } // namespace blocks
} // namespace gr

#endif /* INCLUDED_BLOCKS_QAM_SNR_ESTIMATOR_IMPL_H */

